package com.ruoyi.shiyun.service.impl;

import java.util.ArrayList;

import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.core.domain.TreeSelect;
import com.ruoyi.common.core.domain.entity.SyCategory;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.shiyun.mapper.SyCategoryMapper;
import com.ruoyi.shiyun.service.ISyCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * 商品分类Service业务层处理
 * 
 * @author CX330
 * @date 2024-12-22
 */
@Service
public class SyCategoryServiceImpl implements ISyCategoryService
{
    @Autowired
    private SyCategoryMapper syCategoryMapper;


    /**
     * 查询部门树结构信息
     *
     * @param SyCatgory 商品分类信息
     * @return 商品分类树信息集合
     */
    @Override
    public List<TreeSelect> selectSyCategoryTree(SyCategory SyCatgory) {
        List<SyCategory> list = syCategoryMapper.selectSyCategoryList(SyCatgory);
        return buildCategoryTreeSelect(list);
    }



    /**
     * 构建前端所需要下拉树结构
     *
     * @param list 部门列表
     * @return 下拉树结构列表
     */
    @Override
    public List<TreeSelect> buildCategoryTreeSelect(List<SyCategory> list)
    {
        List<SyCategory> deptTrees = buildCategoryTree(list);
        return deptTrees.stream().map(TreeSelect::new).collect(Collectors.toList());
    }

    /**
     * 构建前端所需要树结构
     *
     * @param list 部门列表
     * @return 树结构列表
     */
    @Override
    public List<SyCategory> buildCategoryTree(List<SyCategory> list)
    {
        List<SyCategory> returnList = new ArrayList<SyCategory>();
        List<Long> tempList = list.stream().map(SyCategory::getCategoryId).collect(Collectors.toList());
        for (SyCategory Category : list)
        {
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(Category.getParentId()))
            {
                recursionFn(list, Category);
                returnList.add(Category);
            }
        }
        if (returnList.isEmpty())
        {
            returnList = list;
        }
        return returnList;
    }

    /**
     * 递归列表
     */
    private void recursionFn(List<SyCategory> list, SyCategory t)
    {
        // 得到子节点列表
        List<SyCategory> childList = getChildList(list, t);
        t.setChildren(childList);
        for (SyCategory tChild : childList)
        {
            if (hasChild(list, tChild))
            {
                recursionFn(list, tChild);
            }
        }
    }


    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<SyCategory> list, SyCategory t)
    {
        return getChildList(list, t).size() > 0;
    }

    /**
     * 得到子节点列表
     */
    private List<SyCategory> getChildList(List<SyCategory> list, SyCategory t)
    {
        List<SyCategory> tlist = new ArrayList<SyCategory>();
        Iterator<SyCategory> it = list.iterator();
        while (it.hasNext())
        {
            SyCategory n = (SyCategory) it.next();
            if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().longValue() == t.getCategoryId().longValue())
            {
                tlist.add(n);
            }
        }
        return tlist;
    }

    /**
     * 查询商品分类
     * 
     * @param categoryId 商品分类主键
     * @return 商品分类
     */
    @Override
    public SyCategory selectSyCategoryByCategoryId(Long categoryId)
    {
        return syCategoryMapper.selectSyCategoryByCategoryId(categoryId);
    }

    /**
     * 查询商品分类列表
     * 
     * @param syCategory 商品分类
     * @return 商品分类
     */
    @Override
    public List<SyCategory> selectSyCategoryList(SyCategory syCategory)
    {
        return syCategoryMapper.selectSyCategoryList(syCategory);
    }

    //统计
    @Override
    public List<SyCategory> selectSyCategoryList2(SyCategory syCategory)
    {
        return syCategoryMapper.selectSyCategoryList2(syCategory);
    }

    /**
     * 新增商品分类
     * 
     * @param syCategory 商品分类
     * @return 结果
     */
    @Override
    public int insertSyCategory(SyCategory syCategory)
    {
        syCategory.setCreateTime(DateUtils.getNowDate());
        return syCategoryMapper.insertSyCategory(syCategory);
    }

    /**
     * 修改商品分类
     * 
     * @param syCategory 商品分类
     * @return 结果
     */
    @Override
    public int updateSyCategory(SyCategory syCategory)
    {
        syCategory.setUpdateTime(DateUtils.getNowDate());
        return syCategoryMapper.updateSyCategory(syCategory);
    }

    /**
     * 批量删除商品分类
     * 
     * @param categoryIds 需要删除的商品分类主键
     * @return 结果
     */
    @Override
    public int deleteSyCategoryByCategoryIds(Long[] categoryIds)
    {
        return syCategoryMapper.deleteSyCategoryByCategoryIds(categoryIds);
    }

    /**
     * 删除商品分类信息
     * 
     * @param categoryId 商品分类主键
     * @return 结果
     */
    @Override
    public int deleteSyCategoryByCategoryId(Long categoryId)
    {
        return syCategoryMapper.deleteSyCategoryByCategoryId(categoryId);
    }
}
